package com.axzhengxin.platform.financeProduct.service.impl;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

import com.axzhengxin.platform.business.domain.ProductArea;
import com.axzhengxin.platform.financeProduct.mapper.ProductAreaMapper;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.mapper.SysDeptMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.axzhengxin.platform.financeProduct.mapper.FspFinanceProductMapper;
import com.axzhengxin.platform.financeProduct.domain.FspFinanceProduct;
import com.axzhengxin.platform.financeProduct.service.IFspFinanceProductService;

import javax.validation.constraints.Size;

/**
 * 金融机构发布产品Service业务层处理
 *
 * @author axzhengxin
 * @date 2021-03-11
 */
@Service
public class FspFinanceProductServiceImpl implements IFspFinanceProductService {
    @Autowired
    private FspFinanceProductMapper fspFinanceProductMapper;
    @Autowired
    private ProductAreaMapper productAreaMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询金融机构发布产品
     *
     * @param fprFlowno 金融机构发布产品ID
     * @return 金融机构发布产品
     */
    @Override
    public FspFinanceProduct selectFspFinanceProductById(String fprFlowno) {

        return fspFinanceProductMapper.selectFspFinanceProductById(fprFlowno);
    }

    /**
     * 查询金融机构发布产品列表
     *
     * @param fspFinanceProduct 金融机构发布产品
     * @return 金融机构发布产品
     */
    @Override
    @DataScope(deptAlias = "sd")
    public List<FspFinanceProduct> selectFspFinanceProductList(FspFinanceProduct fspFinanceProduct) {
        return fspFinanceProductMapper.selectFspFinanceProductList(fspFinanceProduct);
    }

    /**
     * 新增金融机构发布产品
     *
     * @param fspFinanceProduct 金融机构发布产品
     * @return 结果
     */
    @Override
    public int insertFspFinanceProduct(FspFinanceProduct fspFinanceProduct) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String nickName = user.getNickName();
        String fprFinanceid = fspFinanceProduct.getFprFinanceid();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            fprFinanceid = user.getDept().getFinanceId();
        }
        String deptName = fspFinanceProductMapper.getFspDetail(fprFinanceid);
        fspFinanceProduct.setFprFinanceid(fprFinanceid);
        fspFinanceProduct.setFprFinancename(deptName);
        String province = fspFinanceProduct.getProvince();
        String city = "";
        String dis = "";
        if (StringUtils.isNotBlank(province) && !"-1".equals(province)) {
            List<String> strings = Arrays.asList(province.split(","));
            for (String s : strings) {
                if (s.endsWith("00")) {
                    city += s + ",";
                } else {
                    dis += s + ",";
                }
            }
            if (StringUtils.isNotBlank(city)) {
                city = city.substring(0, city.length() - 1);
            }
            if (StringUtils.isNotBlank(dis)) {
                dis = dis.substring(0, dis.length() - 1);
            }
            province = "";
        }
        fspFinanceProduct.setFprUserno(nickName);
        fspFinanceProduct.setCreateBy(user.getUserId().toString());
        int i = fspFinanceProductMapper.insertFspFinanceProduct(fspFinanceProduct);
        String fprFlowno = fspFinanceProduct.getFprFlowno();
        if (i > 0) {
            ProductArea productArea = new ProductArea();
            productArea.setCity(city);
            productArea.setDistrict(dis);
            productArea.setId(fprFlowno);
            productArea.setProvince(province);
            productAreaMapper.insertProductAreaVo(productArea);
        }
        return i;
    }

    /**
     * 修改金融机构发布产品
     *
     * @param fspFinanceProduct 金融机构发布产品
     * @return 结果
     */
    @Override
    public int updateFspFinanceProduct(FspFinanceProduct fspFinanceProduct) {
        Long frpLoanrateother = fspFinanceProduct.getFrpLoanrateother();
        if (frpLoanrateother!=null&&frpLoanrateother==1){
            fspFinanceProduct.setFprBeginrate("0");
            fspFinanceProduct.setFprEndrate("0");
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String fprFinanceid = fspFinanceProduct.getFprFinanceid();
        if (!SecurityUtils.isAdmin(user.getUserId())) {
            fprFinanceid = user.getDept().getFinanceId();
        }
        String deptName = fspFinanceProductMapper.getFspDetail(fprFinanceid);
        fspFinanceProduct.setFprFinanceid(fprFinanceid);
        fspFinanceProduct.setFprFinancename(deptName);
        String province = fspFinanceProduct.getProvince();
        String city = "";
        String dis = "";
        if (StringUtils.isNotBlank(province) && !"-1".equals(province)) {
            List<String> strings = Arrays.asList(province.split(","));
            for (String s : strings) {
                if (s.endsWith("00")) {
                    city += s + ",";
                } else {
                    dis += s + ",";
                }
            }
            if (StringUtils.isNotBlank(city)) {
                city = city.substring(0, city.length() - 1);
            }
            if (StringUtils.isNotBlank(dis)) {
                dis = dis.substring(0, dis.length() - 1);
            }
            province = "";
        }
        int i1 = fspFinanceProductMapper.updateFspFinanceProduct(fspFinanceProduct);
        FspFinanceProduct f = fspFinanceProductMapper.selectFspFinanceProductById(fspFinanceProduct.getFprFlowno());
        String fprFlowno = f.getFprFlowno();
        ProductArea productArea = new ProductArea();
        productArea.setCity(city);
        productArea.setDistrict(dis);
        productArea.setProvince(province);
        productArea.setId(fprFlowno);
        if (i1 > 0) {
            productAreaMapper.updateProductArea(productArea);
        }
        return i1;
    }

    /**
     * 批量删除金融机构发布产品
     *
     * @param fprFlownos 需要删除的金融机构发布产品ID
     * @return 结果
     */
    @Override
    public int deleteFspFinanceProductByIds(String[] fprFlownos) {
        return fspFinanceProductMapper.deleteFspFinanceProductByIds(fprFlownos);
    }

    /**
     * 删除金融机构发布产品信息
     *
     * @param fprFlowno 金融机构发布产品ID
     * @return 结果
     */
    @Override
    public int deleteFspFinanceProductById(String fprFlowno) {
        return fspFinanceProductMapper.deleteFspFinanceProductById(fprFlowno);
    }

    @Override
    public String[] getIds() {


        return fspFinanceProductMapper.getIds();
    }

    @Override
    public List<FspFinanceProduct> getList() {
        return fspFinanceProductMapper.getList();
    }

    @Override
    public List<ProductArea> selectAreaList() {


        return null;
    }
}
